package com.jumi.microservice.service.logistics.express;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jdwl.open.api.sdk.DefaultJdClient;
import com.jdwl.open.api.sdk.JdClient;
import com.jdwl.open.api.sdk.JdException;
import com.jdwl.open.api.sdk.domain.zhongyouex.OrderTraceApi.CommonParam;
import com.jdwl.open.api.sdk.request.zhongyouex.OrderFindTraceInfoByWaybillCodeLopRequest;
import com.jdwl.open.api.sdk.response.zhongyouex.OrderFindTraceInfoByWaybillCodeResponse;
import com.jumi.microservice.dto.logistics.Logistics;
import com.jumi.microservice.dto.logistics.LogisticsSearchRequest;
import com.jumi.microservice.dto.logistics.LogisticsSearchResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author by mazhao
 * 百世快递
 * @Classname BestExpress
 * @Description TODO
 * @Date 2020/8/21 14:30
 */
@Service
public class ZyExpress extends ResponseController implements Express {

    private static final Logger log = LoggerFactory.getLogger(ZyExpress.class);

    /**
     * 测试环境
     */
//    String serverUrl = "https://test-proxy.zhongyouex.com/routerjson";
    /**
     * 正式环境
     */
    private static String serverUrl = "https://api.zhongyouex.com/routerjson";
    private static String accessToken = "556b688a3bf9430dbb3604f9e4bebe77";
    private static String appKey = "113a4f87362a4f9c95af5f8ed097143d";
    private static String appSecret = "25d21c303aec4167843f05a00579e1c9";
    private static String appCode = "EXPRESS_100";
    private static String source = "1111";
    private static String userCode = "mazhao314";
    @Override
    public String expressCode() {
        return "ZY";
    }

    @Override
    public LogisticsSearchResponse logisticsSearch(LogisticsSearchRequest request) {
        log.info("众邮快递查询参数[{}]", JSON.toJSONString(request));
        JSONArray jsonArray = new JSONArray();
        try {
            JdClient client = new DefaultJdClient(serverUrl,accessToken,appKey,appSecret);
            OrderFindTraceInfoByWaybillCodeLopRequest lopRequest = new OrderFindTraceInfoByWaybillCodeLopRequest();
            lopRequest.setWaybillCode(request.getLogisticsNo());
            CommonParam commonParam = new CommonParam();
            commonParam.setAppCode(appCode);
            commonParam.setSource(source);
            commonParam.setUserCode(userCode);
            lopRequest.setCommonParam(commonParam);
            OrderFindTraceInfoByWaybillCodeResponse response = client.execute(lopRequest);
            log.info("请求返回结果[{}]",response.getMsg());
            JSONObject jsonObject = JSONObject.parseObject(response.getMsg());
            jsonArray = jsonObject.getJSONObject("response").getJSONObject("content").getJSONArray("data");
        } catch (JdException e) {
            log.error("众邮快递查询出错[{}]",e.getErrMsg());
        }
        LogisticsSearchResponse logisticsSearchResponse = checkResult(jsonArray,request);
        return controller(logisticsSearchResponse,jsonArray,"content","operateTime");
    }

}
